﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using System.Reflection;
using Newtonsoft.Json;
using IPR_StudyMeetingMrgSys.Models;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 文件类型控制器
    /// </summary>
    public class TrainFileTypeController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);


        /// <summary>
        /// 文件类型的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/TrainFileType/Add")]
        [HttpPost]
        public IHttpActionResult Add( Model.TrainFileType model )
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                BLL.TrainFileType bllfiletype = new BLL.TrainFileType();
                if (!bllfiletype.Exists(model.FTName))
                {
                    if (new IPR_StudyMeetingMrgSys.BLL.TrainFileType().Add(model) > 0)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = "文件类型增加成功！";
                    }
                    else
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "文件类型增加失败！";
                    }
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "文件类型已经存在，请不要重复！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "文件类型增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 文件类型查询
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="startindex"></param>
        /// <param name="endindex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainFileType/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        { 
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrEmpty(swhere == null ? string.Empty : swhere.Trim()))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = string.Empty;
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "FTName")
                        {
                            swhere += $" and FTName like '%{item.Value}%'";
                        }
                    }
                }
                int rowCount = 0;
                bool blExist = false;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainFileType().GetListByPage(swhere, "FTID", startindex, endindex,out rowCount);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
                    resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
                    resultMsg.Data = "查询结果不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 文件类型编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainFileType/Edit")]
        public IHttpActionResult Edit(Model.TrainFileType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileType().Update(model))
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "文件类型编辑成功！";
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "文件类型编辑失败！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "文件类型编辑时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/TrainFileType/Del")]
        public IHttpActionResult Del(Model.TrainFileType model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileType().Delete(model.FTID))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "文件类型删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "文件类型删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "文件类型删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
        [HttpGet]
        [Route("api/TrainFileType/DelList")]
        public IHttpActionResult DelList(string FTIDList)
        {
            log.Info(JsonConvert.SerializeObject(FTIDList));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainFileType().DeleteList(FTIDList))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "文件类型删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "文件类型删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "文件类型删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/TrainFileType/Sel")]
        public IHttpActionResult GetDepList()
        {
            log.Info($"获取文件类型列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = new IPR_StudyMeetingMrgSys.BLL.TrainFileType().GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到文件类型信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        // GET: api/FileType
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/FileType/5
        public string Get(int id)
        {
            Model.TrainFileType mft = new Model.TrainFileType();
            mft = new BLL.TrainFileType().GetModel(id);
            return JsonConvert.SerializeObject(mft);
        }

        // POST: api/FileType
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/FileType/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/FileType/5
        public void Delete(int id)
        {
        }
    }
}
